package com.js.nickcar.api;

import com.zyzd.commontools.encrypt.MD5Utils;
import com.zyzd.commontools.log.TLog;
import com.zyzd.commontools.screen.TDevice;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by LZY on 2017/9/28.
 */

public class UrlUtils implements UrlConfig{

    /**
     * 将api转成url
     *
     * @param api
     * @return
     */
    public static String getRequestUrl(String api) {
        return BASE_URL + api;
    }

    /**
     * 获取基本请求参数
     *
     * @param api
     * @return
     */
    public static Map<String, String> getBaseRequestMap(String api) {
        HashMap<String, String> map = new HashMap<>();
        map.put("timestamp", UserHelper.getTimeStamp() + "");
        map.put("id", UserHelper.getUserId());
        map.put("sign", getSign(getRequestUrl(api), map));
        return map;
    }

    /**
     * 获取签名
     *
     * @param url
     * @param map
     * @return
     */
    private static String getSign(String url, HashMap<String, String> map) {
        StringBuffer sb = new StringBuffer(url);
        sb.append("?");
        sb.append(map.get("id")).append("&");
        sb.append(UserHelper.getToken()).append("&");
        sb.append(map.get("timestamp"));
        TLog.d("zyzd", "UrlUtil>>getSign-原文-> " + sb.toString());
        return MD5Utils.getMd5(sb.toString());
    }

    /**
     * 指定高度获取图片地址
     *
     * @param baseImageUrl 基本路径
     * @param heightDp     高度dp值
     * @return
     */
    public static String getImageUrlByHeight(String baseImageUrl, int heightDp) {
        return baseImageUrl + "?h=" + TDevice.dpToPx(heightDp);
    }

    /**
     * 指定宽度度获取图片
     *
     * @param baseImageUrl 基本路径
     * @param widthDp      宽度dp值
     * @return
     */
    public static String getImageUrlByWidth(String baseImageUrl, int widthDp) {
        return baseImageUrl + "?w=" + TDevice.dpToPx(widthDp);
    }
}
